﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using DrivingCRM.Controller;
using DrivingCRM.Model;

namespace DrivingCRM.Views.Private.CRM
{
    public partial class AddUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.RDP_DateOfBirth.SelectedDate = DateTime.Now;

            this.DDL_UserType.SelectedIndexChanged += new EventHandler(DDL_UserType_SelectedIndexChanged);
            this.MV_UserView.ActiveViewIndex = 0;
            this.MV_Agency.ActiveViewIndex = 1;
        }

        /// <summary>
        /// Méthode déclenchée au changement dans la combobox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DDL_UserType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.DDL_UserType.SelectedValue == "stakeholder")
            {
                this.MV_UserView.ActiveViewIndex = 2;
                this.MV_Agency.ActiveViewIndex = 1;
            }
            else if (this.DDL_UserType.SelectedValue == "client")
            {
                this.MV_UserView.ActiveViewIndex = 0;
                this.MV_Agency.ActiveViewIndex = 1;
            }
            else if (this.DDL_UserType.SelectedValue == "salesman")
            {
                this.MV_UserView.ActiveViewIndex = 1;
                this.MV_Agency.ActiveViewIndex = 1;
            }
            else
            {
                this.MV_UserView.ActiveViewIndex = 1;
                this.MV_Agency.ActiveViewIndex = 0;
            }
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de création
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_CreateUser_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
                MembershipCreateStatus status;
                EFMembershipUser user = null;

                switch (this.DDL_UserType.SelectedValue)
                {
                    case "client":
                        /// tentative de création de l'adresse
                        Address linkedAddress = EntityManager.AddAdress(this.Number.Text, int.Parse(this.StreetType.SelectedValue), this.StreetName.Text, this.Extra.Text, this.Zipcode.Text, this.City.Text, this.Cedex.Text);

                        /// si l'adresse a été ou existe déjà
                        if (linkedAddress != null)
                        {
                            /// tentative d'ajout de l'utilisateur
                            user = ((EFMembershipProvider)Membership.Provider).CreateClient(this.Firstname.Text,
                                this.Lastname.Text,
                                this.UserName.Text,
                                this.Password.Text,
                                this.Email.Text,
                                this.DDL_Question.SelectedValue,
                                this.Answer.Text,
                                true,
                                null,
                                out status,
                                this.RDP_DateOfBirth.SelectedDate.Value, 0, 0, linkedAddress, int.Parse(this.DDL_Agencies.SelectedValue));
                        }

                        break;
                    case "salesman":
                        user = ((EFMembershipProvider)Membership.Provider).CreateSalesman(this.Firstname.Text,
                            this.Lastname.Text,
                            this.UserName.Text,
                            this.Password.Text,
                            this.Email.Text,
                            this.DDL_Question.SelectedValue,
                            this.Answer.Text,
                            true,
                            null,
                            out status,
                            this.RDP_DateOfBirth.SelectedDate.Value,
                            0,
                            int.Parse(this.DDL_Agencies.SelectedValue));
                        break;
                    case "stakeholder":
                        user = ((EFMembershipProvider)Membership.Provider).CreateStakeholder(this.Firstname.Text,
                            this.Lastname.Text,
                            this.UserName.Text,
                            this.Password.Text,
                            this.Email.Text,
                            this.DDL_Question.SelectedValue,
                            this.Answer.Text,
                            true,
                            null,
                            out status,
                            this.RDP_DateOfBirth.SelectedDate.Value,
                            true,
                            true,
                            int.Parse(this.DDL_Agencies.SelectedValue));
                        break;
                    case "administrator":
                        user = ((EFMembershipProvider)Membership.Provider).CreateAdministrator(this.Firstname.Text,
                            this.Lastname.Text,
                            this.UserName.Text,
                            this.Password.Text,
                            this.Email.Text,
                            this.DDL_Question.SelectedValue,
                            this.Answer.Text,
                            true,
                            null,
                            out status,
                            this.RDP_DateOfBirth.SelectedDate.Value);
                        break;
                    default:
                        break;
                }

                if (user != null)
                    this.ErrorMessage.Text = "Ajout de l'utilisateur effectué";
            }
        }
    }
}